#include"nlohmann/json.hpp"
#include"Configuration.h"
#include<iostream>
#include<fstream>
using std::ifstream;
using json = nlohmann::json;
Configuration* Configuration::_instanceConfig=nullptr;
Configuration::Configuration(string filename){
    // 读取 JSON 文件
    std::ifstream file(filename);
    if (!file.is_open()) {
        std::cerr << "Can't open " << filename << "for reading" <<"\n";
        return;
    }

    json config;
    file >> config;
    file.close();

    // 访问配置项
    if(config.is_null()){
        std::cerr<<"empty config\n";
    }
    if (config.contains("page_files") && config["page_files"].is_array()) {
        const auto& text_files = config["page_files"];
        for (const auto& path : text_files) {
            _configs["page_files"]=path;
        }
    }
    if (config.contains("pagelib") && config["pagelib"].is_array()) {
        const auto& text_files = config["pagelib"];
        for (const auto& path : text_files) {
            _configs["pagelib"]=path;
        }
    }
    if (config.contains("offset") && config["offset"].is_array()) {
        const auto& text_files = config["offset"];
        for (const auto& path : text_files) {
            _configs["offset"]=path;
        }
    }
}
Configuration* Configuration::getInstance(){
    if(_instanceConfig==nullptr){
        _instanceConfig=new Configuration("/home/zzh/cpp/project/v1_project/search/conf/myconf.json");
        
    }
    return _instanceConfig;
}
map<string,string>& Configuration::getConfigMap(){
    return _configs;
}
